package com.company.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.company.psm.entity.Store;
import com.company.psm.service.StoreManagement;

@Controller
@RequestMapping("/store")
public class StoreController {
	@Autowired
	private StoreManagement storeManagement;
	@RequestMapping(method=RequestMethod.GET,value = "list")
	public ModelAndView getAll(@RequestParam(value="listAll", required=false) String listAll){
		List<Store> stores = new ArrayList<Store>();
		if (listAll!=null && listAll.equals("listAllData")){
			stores = storeManagement.getAll();
		}
		return new ModelAndView("/store/list", "stores", stores);
	}
	@RequestMapping(value = "create",method = RequestMethod.GET)
	public ModelAndView addStore(@RequestParam(value="createName", required=false) String name,
			@RequestParam(value="createAddress", required=false) String address,
			@RequestParam(value="createPhone", required=false) String phone,
			@RequestParam(value="createStatus", required=false) String status){
		storeManagement.add(name, address, phone, status);
		return new ModelAndView("/store/list", "stores", storeManagement.getAll());
	}
	@RequestMapping(value = "update",method = RequestMethod.GET)
	public ModelAndView updateStore(@RequestParam(value="updateStoreId", required=false) String id,
			@RequestParam(value="updateName", required=false) String name,
			@RequestParam(value="updateAddress", required=false) String address,
			@RequestParam(value="updatePhone", required=false) String phone,
			@RequestParam(value="updateStatus", required=false) String status){
		System.err.println("StoreID = "+id);
		String storeId = String.valueOf(id).split(",")[0];
		storeManagement.update(storeId,name, address, phone, status);
		return new ModelAndView("/store/list", "stores", storeManagement.getAll());
	}
}
